<template>
	<view class="box">
		<view class="list-cell-user" v-for="(item, index) in contentList"
			@click="item.msgType === 0 ? emptyFunction() : toDetail(item.postId)">
			<view class="user">
				<image class="avatar" :src="item.avatar" mode="aspectFill"
					style="height: 70rpx;width: 70rpx;border-radius: 10rpx;margin:0 35rpx 0 0;">
				</image>
				<view class="name">
					{{item.username}}
				</view>
				<view class="time">
					{{item.time}}
				</view>
			</view>

			<!-- 新增一个view用于包裹content、agree和disagree，实现它们的水平布局 -->
			<view class="content-agree-disagree">
				<view class="content">
					{{item.content}}
				</view>
				<view class="actions" v-if="(item.msgType == 0) && (item.content !== '已同意' && item.content !== '已拒绝')">
					<view class="agree" @click="agree(index)">同意</view>
					<view class="disagree" @click="disagree(index)">拒绝</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		listMsg,
		updateMessage
	} from "@/api/message";

	import {
		addUserFriend
	} from "@/api/friend";

	export default {

		data() {
			return {
				// 论坛主体
				contentList: [{
					id: 0,
					postId: 0,// 用于贴子评论消息的跳转
					userId: 0,
					friendId: 0,
					avatar: '',
					username: '',
					content: '',
					time: '',
					msgType: 0 // 0是好友申请，1是评论信息
				}],
			};
		},
		onLoad() {
			this.getMsgList()
		},
		methods: {
			// 获取当前用户收到的消息
			getMsgList() {
				this.loading = true
				var msg = {
					
				}
				listMsg(msg).then(response => {
					console.log(response.data);
					// 为消息集合赋值
					this.contentList = response.data
					this.loading = false
				})
			},
			// 同意加好友
			agree(index) {
				// content赋值为已同意
				// 数据库中建立好友关系
				var parms = {
					userId: this.contentList[index].userId,
					friendId: this.contentList[index].friendId,
				};
				addUserFriend(parms).then(response => {
					// console.log(response);
					if (response.msg == "好友关系已存在") {
						this.$modal.msgError("好友关系已存在");
					} else {
						// 数据库修改当前请求content
						var data = {
							id: this.contentList[index].id,
							content: "已同意"
						};
						// 更新当前消息界面数据
						updateMessage(data).then(response => {
							this.$modal.msgSuccess("添加成功");
							this.$tab.redirectTo("/pages/community/msg/msg");
						})
					}
				})
			},
			// 不同意加好友
			disagree(index) {
				// content赋值为已拒绝
				var parms = {
					id: this.contentList[index].id,
					content: "已拒绝"
				};
				// 数据库修改当前请求content
				updateMessage(parms).then(response => {
					this.$modal.msgSuccess("已拒绝");
					this.$tab.redirectTo("/pages/community/msg/msg")
				})
			},

			// 跳转到帖子详情界面
			toDetail(postId) {
				wx.navigateTo({
					url: "/pages/community/detail/detail?postId=" + postId
				})
			},
			// 当前item是好友请求，不跳转
			emptyFunction() {
			}
		}
	}
</script>

<style lang="scss">
	.box {

		// 论坛主体
		.list-cell-user {
			display: flex;
			align-items: center;
			box-sizing: border-box;
			width: 100%;
			padding: 10rpx 24rpx;
			overflow: hidden;
			color: #323233;
			font-size: rpx;
			line-height: 24rpx;
			background-color: #fff;
			flex-direction: column;
			margin: 15rpx 0;

			.user {
				display: flex;
				align-items: center;
				width: 100%;

				.avatar {
					height: 70rpx;
					width: 70rpx;
					border-radius: 10rpx;
					margin-right: 35rpx;
				}

				.name {
					flex: 1;
					margin-left: 10rpx;
					font-size: 40rpx;
					font-weight: 500;
				}

				.time {
					text-align: right;
					margin-top: 5rpx;
					color: gray;
				}
			}

			.content-agree-disagree {
				display: flex;
				justify-content: space-between;
				align-items: flex-start;
				width: 100%;
				margin-top: 10rpx;

				.content {
					width: 100%;
					flex: 1;
					margin-top: 10rpx;
					text-align: left;
					font-size: 35rpx;
					margin: 20rpx 0;
					padding: 15rpx 0 0 25rpx;
				}

				.actions {
					height: 80rpx;
					display: flex;
					align-items: center;

					.agree {
						height: 100%;
						width: 150rpx;
						margin: 0 10rpx;
						border-radius: 20rpx;
						font-size: 45rpx;
						font-weight: 500;
						color: white;
						background-color: seagreen;

						display: flex;
						align-items: center;
						justify-content: center;
						text-align: center;
					}

					.disagree {
						height: 100%;
						width: 150rpx;
						margin: 0 10rpx;
						border-radius: 20rpx;
						font-size: 45rpx;
						font-weight: 500;
						color: white;
						background-color: red;

						display: flex;
						align-items: center;
						justify-content: center;
						text-align: center;
					}
				}
			}
		}
	}
</style>